package com.jd.org.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.ExcelUtils;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Shop;
import com.jd.org.domain.ShopAuditLog;
import com.jd.org.dto.shopDto;
import com.jd.org.query.ShopQuery;
import com.jd.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API", description = "店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public AjaxResult del(@PathVariable("id") Long id) {
        shopService.del(id);
        return new AjaxResult();
    }

    /**
     * 添加或者修改
     *
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        if (shop.getId() == null) {
            shopService.add(shop);
        } else {
            shopService.update(shop);
        }
        return new AjaxResult();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        shopService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 分页查询
     *
     * @param shopQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.queryPage(shopQuery);
    }

    /**
     * 店铺入驻业务
     *
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入职")
    public AjaxResult settlement(@RequestBody Shop shop) {
        shopService.settlement(shop);
        return new AjaxResult();
    }

    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审批通过")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        shopService.auditPass(shopAuditLog);
        return new AjaxResult();
    }

    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审批驳回")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        shopService.auditReject(shopAuditLog);
        return new AjaxResult();
    }
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入")
    public void importExcel(@RequestPart("file")MultipartFile file) {
        ExcelUtils.importExcel(file,1,1,Shop.class);
    }
    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> list = shopService.findAll();
        ExcelUtils.exportExcel(list,null,"店铺导出数据",Shop.class,"店铺数据.xlsx",response);
    }

    @GetMapping("/columnEcharts")
    @ApiOperation(value = "统计每种状态店铺数量")
    public List<shopDto> columnEcharts() {
        List<shopDto> list = shopService.columnEcharts();
        return list;
    }

}
